//
//  YCCommentView.m
//  LOLBox
//
//  Created by mac on 15-3-23.
//  Copyright (c) 2015年 Honey. All rights reserved.
//

#import "YCCommentView.h"
#import "YCCommentModel.h"
#import "UIImageView+WebCache.h"

@interface YCCommentView ()
{
    UIImageView *_imgView;
    UILabel *_username;
    UILabel *_content;
    UILabel *_rightLabel;
    UILabel *_floor;
}

@end

@implementation YCCommentView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        // 60
        //self.frame = CGRectMake(0, 0, 320, 50);
        _imgView = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 40, 40)];
        _username = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(_imgView.frame) + 5, 5, 200 , 20)];
        _content = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(_imgView.frame) + 5, CGRectGetMaxY(_username.frame) + 2, 240, 30)];
        _rightLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetWidth(self.frame) - 35, 15, 30, 30)];
        
        
        //_imgView.backgroundColor = [UIColor blueColor];
        //_username.backgroundColor = [UIColor yellowColor];
        //_content.backgroundColor = [UIColor lightGrayColor];
        //_rightLabel.backgroundColor = [UIColor magentaColor];
        
        _content.numberOfLines = 0;
        
        _username.font = [UIFont systemFontOfSize:13];
        _rightLabel.font = [UIFont systemFontOfSize:14];
        _content.font = [UIFont systemFontOfSize:13];
        _content.textColor = [UIColor grayColor];
        
        [self addSubview:_imgView];
        [self addSubview:_username];
        [self addSubview:_content];
        [self addSubview:_rightLabel];
    }
    return self;
}

- (CGFloat)fillCellWithModel:(YCCommentModel *)m
{
    _username.text = m.username;
    _content.text = m.content;
    _rightLabel.text = [NSString stringWithFormat:@"%@顶",m.goodNum];
    [_imgView sd_setImageWithURL:[NSURL URLWithString:m.icon] placeholderImage:[UIImage imageNamed:@"noimage_xiangqing"]];
    
    // content自适应高度
    CGFloat height = [self setRowHeight:m.content];
    
    CGRect frame = _content.frame;
    frame.size.height = height;
    _content.frame = frame;
    
    // 每行的下划线
    UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_content.frame) + 5, CGRectGetWidth(self.frame), 1)];
    v.backgroundColor = [UIColor lightGrayColor];
    v.alpha = 0.8;
    [self addSubview:v];
    
    return height;
}

- (CGFloat)setRowHeight:(NSString *)s
{
//    CGSize size = [s sizeWithFont:[UIFont systemFontOfSize:13] constrainedToSize:CGSizeMake(280, 1000)];
    NSDictionary *attrs = @{NSFontAttributeName:[UIFont systemFontOfSize:13]};
    CGRect frame = [s boundingRectWithSize:CGSizeMake(240, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil];
    return CGRectGetHeight(frame) ;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
